package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.TPlatformfileMapper;
import com.hbnu.system.model.entity.TPlatform;
import com.hbnu.system.model.entity.TPlatformfile;
import com.hbnu.system.service.IUserService;
import com.hbnu.system.service.TPlatformService;
import com.hbnu.system.service.TPlatformfileService;
import com.hbnu.system.utils.CheckRoleUtil;
import com.hbnu.system.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.BindException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service("tplatformfileService")
public class TPlatformfileServiceImpl extends ServiceImpl<TPlatformfileMapper, TPlatformfile> implements TPlatformfileService {

    @Autowired
    FileUploadUtil fileUploadUtil;
    @Autowired
    TPlatformService tPlatformService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private CheckRoleUtil checkRoleUtil;

    /**
     * 添加文件
     * @param tPlatformfile 文件对象
     * @return 操作结果
     */
    @Override
    public boolean addTPlatformfile(TPlatformfile tPlatformfile) {
        TPlatform tPlatform = tPlatformService.getById(tPlatformfile.getTplatformid());
        System.out.println("tPlatform="+tPlatform);
        checkRoleUtil.checkPermissionProject(tPlatform.getStatus());

//        tPlatformfile.setFiledate(LocalDateTime.now());
        tPlatformfile.setFiledate(LocalDateTime.now());
        tPlatformfile.setFlag(1);
        if(baseMapper.insert(tPlatformfile) != 1)
            throw new BaseException("上传失败,请重新尝试");
        return true;
    }


    @Override
    public List<TPlatformfile> pageTPlatformfile(String id) {
        if(StrUtil.isBlank(id)){
            throw new BaseException("著作编号为空值");
        }
        List<TPlatformfile> tPlatformfileList = baseMapper.selectList(Wrappers.lambdaQuery(TPlatformfile.class)
                .eq(TPlatformfile::getTplatformid,id));
        return tPlatformfileList;
    }

    @Override
    public boolean deletTPlatformfile(String id) throws IOException {
        TPlatformfile one = super.getOne(Wrappers.lambdaQuery(TPlatformfile.class)
                .eq(TPlatformfile::getFileid, id));
        if (Objects.isNull(one)){
            throw new BaseException("文件编号有误!");
        }

        TPlatform tPlatform = tPlatformService.getById(one.getTplatformid());
        checkRoleUtil.checkPermissionProject(tPlatform.getStatus());

        //做flag的判断
        if(one.getFlag() == 0){
            throw new BaseException("不可删除该文件!");
        }


        boolean delete = fileUploadUtil.delete(one.getFileurl());
        if(!delete){
            throw new BindException("附件删除失败");
        }
        return baseMapper.deleteById(id) == 1;
    }
}
